﻿/*
Copyright (c) 2008 Martin Legris

Permission is hereby granted, free of charge, to any person obtaining a copy of this 
software and associated documentation files (the "Software"), to deal in the Software 
without restriction, including without limitation the rights to use, copy, modify, 
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 
permit persons to whom the Software is furnished to do so, subject to the following 
conditions:

The above copyright notice and this permission notice shall be included in all copies 
or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

@ignore
*/
package newcommerce.lastfm.api 
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import newcommerce.lastfm.api.clients.ArtistClient;
	import newcommerce.lastfm.api.clients.AuthClient;
	import newcommerce.lastfm.api.clients.LibraryClient;
	import newcommerce.lastfm.api.clients.ScrobbleClient;
	import newcommerce.lastfm.api.events.LFMArtistInfoEvent;
	import newcommerce.lastfm.api.events.LFMEventFeedEvent;
	import newcommerce.lastfm.api.events.LFMLibraryAddEvent;
	import newcommerce.lastfm.api.events.LFMScrobbleEvent;
	import newcommerce.lastfm.api.events.LFMSessionEvent;
	import newcommerce.lastfm.api.events.LFMShareEvent;
	import newcommerce.lastfm.api.events.LFMSimilarArtistEvent;
	import newcommerce.lastfm.api.events.LFMTopAlbumsEvent;
	import newcommerce.lastfm.api.feeds.LFMSimilarArtistFeed;
	
	/**
	 * ...
	 * @author Martin Legris ( http://blog.martinlegris.com )
	 */
	public class LastFMTest extends MovieClip
	{
		protected var _client:LastFMClient;
		protected var _username:String = "username";
		protected var _password:String = "password";

		public function LastFMTest() 
		{
			_client = LastFMClient.getInstance();

			initScrobbleEvents();

			var artist:ArtistClient = _client.artist;
			var auth:AuthClient = _client.auth;
			var library:LibraryClient = _client.library;

			library.addEventListener(LFMLibraryAddEvent.ALBUM_ADDED, doAlbumAdded);
			library.addEventListener(LFMLibraryAddEvent.ALBUM_ADD_FAILED, doAlbumAddFailed);
			library.addEventListener(LFMLibraryAddEvent.ARTIST_ADDED, doArtistAdded);
			library.addEventListener(LFMLibraryAddEvent.ARTIST_ADD_FAILED, doArtistAddFailed);
			library.addEventListener(LFMLibraryAddEvent.TRACK_ADDED, doTrackAdded);
			library.addEventListener(LFMLibraryAddEvent.TRACK_ADD_FAILED, doTrackAddFailed);

			artist.addEventListener(LFMSimilarArtistEvent.FEED_READY, doSimilarFeedReady);			
			artist.addEventListener(LFMArtistInfoEvent.DATA_READY, doArtistInfoReady);
			artist.addEventListener(LFMTopAlbumsEvent.FEED_READY, doTopAlbumsReady);
			artist.addEventListener(LFMShareEvent.SUCCESS, doArtistShared);
			artist.addEventListener(LFMEventFeedEvent.FEED_READY, doEventFeedReady);
			artist.addEventListener(LFMEventFeedEvent.REQUEST_FAILED, doEventFeedFailed);			
			auth.addEventListener(LFMSessionEvent.AUTH_FAILED, doAuthFailed);
			auth.addEventListener(LFMSessionEvent.DATA_READY, doSessionReady);

			//artist.getEvents("Camille");
			// artist.getSimilar("Camille");
			//artist.getInfo("Camille");
			//artist.getTopAlbums("Camille");

			auth.getMobileSession(_username, _password);
		}
		
		protected function initScrobbleEvents():void
		{
			var scrobble:ScrobbleClient = _client.scrobble;
			scrobble.addEventListener(LFMScrobbleEvent.HANDSHAKE_SUCCESS, doHandShakeSuccess);
			scrobble.addEventListener(LFMScrobbleEvent.NOW_PLAYING_SUCCESS, doNowPlayingSuccess);
			scrobble.addEventListener(LFMScrobbleEvent.SUBMIT_ONE_SUCCESS, doSubmitOneSuccess);
		}
		
		protected function doSimilarFeedReady(evt:LFMSimilarArtistEvent):void
		{
			var feed:LFMSimilarArtistFeed = evt.feed;
			LastFMTracer.traceSimilarArtists(feed);
		}
		
		protected function doSessionReady(evt:LFMSessionEvent):void
		{
			trace(evt.session.name + " has logged in");
			
			//_client.library.addTrack("Nacho Sotomayor", "Don't Do Anything");
			//_client.library.addArtist("Sum 41");
			//_client.library.addAlbum("Sum 41", "Does This Look Infected");
			
			_client.scrobble.handshake(_username);
			
			//_client.artist.share("Camille", ["martin.legris@gmail.com"], "Hi check her out!");
			
			//_client.removeArtistTag("Rise Against", "emo");
			//_client.addArtistTags("Rise Against", ["punk rock","energetic","symphonic","emo"]);
			//_client.playlistAddTrack(4189616, "Augustana", "Reasons");			
			//_client.createPlaylist("playlist 1", "my first playlist");
		}
		
		protected function doArtistInfoReady(evt:LFMArtistInfoEvent):void
		{
			trace("artistInfoReady");
			LastFMTracer.traceArtistInfo(evt.artistInfo);
		}
		
		protected function doAuthFailed(evt:LFMSessionEvent):void
		{
			
		}
		
		protected function doTopAlbumsReady(evt:LFMTopAlbumsEvent):void
		{
			trace("topAlbumsReady");
			LastFMTracer.traceAlbums(evt.feed);
		}
		
		protected function doArtistShared(evt:LFMShareEvent):void
		{
			trace("artist shared!");
		}
		
		protected function doEventFeedReady(evt:LFMEventFeedEvent):void
		{
			trace("eventFeedReady");
			LastFMTracer.traceEvents(evt.feed);
		}
		
		protected function doEventFeedFailed(evt:LFMEventFeedEvent):void
		{
			trace("eventFeedFailed");
		}
		
		protected function doTrackAddFailed(evt:LFMLibraryAddEvent):void
		{
			trace("failed to add track");
		}
		
		protected function doTrackAdded(evt:LFMLibraryAddEvent):void
		{
			trace("track added to library");
		}
		
		protected function doAlbumAdded(evt:LFMLibraryAddEvent):void
		{
			trace("album added to library");
		}
		
		protected function doAlbumAddFailed(evt:LFMLibraryAddEvent):void
		{
			trace("failed to add album");
		}
		
		protected function doArtistAdded(evt:LFMLibraryAddEvent):void
		{
			trace("artist added to library");
		}
		
		protected function doArtistAddFailed(evt:LFMLibraryAddEvent):void
		{
			trace("failed to add artist");
		}
		
		protected function doHandShakeSuccess(evt:LFMScrobbleEvent):void
		{
			trace("handshake sccess...");
			
			// TESTING NOW PLAYING
			//_client.scrobble.nowPlaying("Pierre Lapointe", "Extra", 16, "La forêt des mal-aimés", 882);	
			
			// TESTING SUBMIT ONE
			var date:Date = new Date();
			date.minutes -= 5;
			_client.scrobble.submitOne("Pierre Lapointe", "Extra", 882, date);
		}
		
		protected function doNowPlayingSuccess(evt:LFMScrobbleEvent):void
		{
			trace("now playing success [" + evt.wrapper.artist + " / " + evt.wrapper.track + "]");
		}
		
		protected function doSubmitOneSuccess(evt:LFMScrobbleEvent):void
		{
			trace("submit one success [" + evt.wrapper.artist +" / " + evt.wrapper.track + "]");
		}
	}
}